# -*- coding: UTF-8 -*-
from bs4 import BeautifulSoup
import requests
import re
import datetime
import smtplib
from email.mime.text import MIMEText
from email.header import Header
url ="https://jwc.zafu.edu.cn/"
list_date=[]
list_title=[]
list_href=[]

from_addr = '**********@qq.com' #发送的邮箱地址，默认为QQ邮箱
password = '*******' #邮箱密钥，需要开通STMP，注意此密钥不是邮箱的登录密码
smtp_server = 'smtp.qq.com' #stmp服务器地址
to_addr = '**********@qq.com' #接受者的邮箱地址
filename="update"+to_addr+'.txt'
html = requests.get(url)
soup = BeautifulSoup(html.content, 'lxml')
for i in soup.find_all(attrs={'class': 'tzgg_nr'}):
    for a in i.select('a'):
        list_title.append(a.get_text())
    for li in i.select('span'):
        list_date.append(li.get_text())
    for href in i.select('a'):
        list_href.append(href.get('href'))

with open(filename,"r") as f: #需要在目录下新建一个 upadtexxxxxxxxx@qq.com.txt 的文本文档
        sflag=f.readline()

print(list_date[0])
print(list_title[0])
print(list_href[0])
if sflag!=list_title[0]:
    msg = MIMEText('发布日期为：'+list_date[0]+' 更新内容为：'+list_title[0]+'链接:https://jwc.zafu.edu.cn/'+list_href[0],'plain','utf-8')
    msg['From'] = Header(from_addr)
    msg['To'] = Header(to_addr)
    msg['Subject'] = Header('教务处通知更新提醒')
    server=smtplib.SMTP_SSL(smtp_server) 
    server.connect(smtp_server,465)

    server.login(from_addr, password)
    server.sendmail(from_addr, to_addr, msg.as_string())
    with open(filename,"w") as f:
        f.writelines(list_title[0])   
    server.quit()
    

        


